<?php
namespace Home\Controller;

class OrderController extends HomeController
{

    /**
     * 添加购物车
     *
     * @author Ringo Chan <ringochan@jxlnxx.com>
     */
    public function addCart ()
    {
        !is_login() && $this->referer('User/login', $_SERVER['HTTP_REFERER']);
            $quantity = I('post.quantity');
            $product_id = I('post.product_id');
            
            $M_product=M("product");
            $M_cart=M('cart');
            $product_info=$M_product->find($product_id);
            
            
            $data=array();
            $data['quantity']=$quantity;
            $data['price']=$product_info['price'];
            $data['total_price']=$quantity*$product_info['price'];
            $data['product_id']=$product_id;
            $data['uid']=UID;
            $data['create_time']=NOW_TIME;
            $data['update_time']=NOW_TIME;
            $data['status']=1;
            $data['option_ids']=0;
            $data['product_attr']='默认';
            $map=array('product_id'=>$data['product_id'],'uid'=>UID, 'status'=>1);
            $exist=$M_cart->where($map)->find();
            $result=0;
            if($exist){
                $result=$M_cart->where($map)->setInc('quantity',$data['quantity']);
                $M_cart->where($map)->setInc('price',$data['price']);
            }else{
                $result=$M_cart->add($data);
            }
            
            $this->assign('info',$exist);
            $this->display();            
        
    }

    /**
     * 购物车列表
     *
     * @author Ringo Chan <ringochan@jxlnxx.com>
     */
    public function carts ()
    {
        !is_login() && $this->referer('User/login',U('Order/carts'));
        valid_login();        
        $M_cart=M('cart');
        $M_product_image = M('product_image');
        $M_shop=M('shop');
        $M_product=M('product');
      
        $prefix = C('DB_PREFIX');
        $l_table = $prefix.('product');
        $r_table = $prefix.('cart');
        
        $model = M('cart');
        
        $model = $model->table($l_table.' l')
        ->join($r_table . ' r on r.product_id=l.id');
        
         $options['field'] = array('l.title ', 'l.shop_id','l.uid seller_uid',
             'l.price product_price','r.price','r.total_price',
             'r.product_id ','r.id cart_id','r.quantity','r.uid','r.status');
        $OPT = new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true); 
        
        $map=array();
        $map['r.uid'] = UID;
        $map['r.status']=1;
        
        $options['where']=$map;
        
        $options = array_merge( (array)$OPT->getValue($model), $options );
        
        $model->setProperty('options',$options);
        
        $carts_list=$model->select();
        $cart_result=array();
        
    foreach ($carts_list as $ca_item){
        
            $tem_shop=$M_shop->find($ca_item['shop_id']);
        
            $tem_cart_item=array();
            $tem_cart_item['cart_id']=$ca_item['cart_id'];
            
            $tem_product=array();
            $tem_product['title']=$ca_item['title'];
            $tem_product['total_price']=$ca_item['total_price'];
            $tem_product['price']=$ca_item['price'];
            $tem_product['quantity']=$ca_item['quantity'];
            $tem_product['product_id']=$ca_item['product_id'];
            $tem_product['option_ids']=$ca_item['option_ids'];
            $tem_product['category_id']=$ca_item['category_id'];
            $tem_product['product_price']=$ca_item['product_price'];
            $tem_product['cart_id']=$ca_item['cart_id'];
             
            $images = $M_product_image->where(array('product_id'=>$ca_item['product_id']))->find();
            $tem_product['image_path']= get_cover($images['cover_id'],'path');
            
            if($tem_shop){
                $tem_cart_item['shop_id']=$ca_item['shop_id'];
                $tem_cart_item['shop_title']=$tem_shop['title'];                
                
                $cart_result[$ca_item['shop_id']]=$tem_cart_item; 
                
            }else{
                $tem_cart_item['shop_id']=0;
                $tem_cart_item['shop_title']='闲置商品卖家：'.get_username($ca_item['seller_uid']);
                
                $tem_cart_item['product'][]=$tem_product;
                array_push($cart_result,$tem_cart_item);
            }             
        }
        foreach ($carts_list as $ca_item){
            $tem_product=array();
            $tem_product['title']=$ca_item['title'];
            $tem_product['total_price']=$ca_item['total_price'];
            $tem_product['price']=$ca_item['price'];
            $tem_product['quantity']=$ca_item['quantity'];
            $tem_product['product_id']=$ca_item['product_id'];
            $tem_product['option_ids']=$ca_item['option_ids'];
            $tem_product['category_id']=$ca_item['category_id'];
            $tem_product['product_price']=$ca_item['product_price'];
            $tem_product['cart_id']=$ca_item['cart_id'];
            $images = $M_product_image->where(array('product_id'=>$ca_item['product_id']))->find();
            $tem_product['image_path']= get_cover($images['cover_id'],'path');
            
            if($ca_item['shop_id']){
                $cart_result[$ca_item['shop_id']]['product'][]=$tem_product;
            }
        }
        $this->assign('_list',$cart_result);
        $this->display();
    }

    /**
     * 确认订单
     * @author Ringo Chan <ringochan@jxlnxx.com>
     */
    public function save()
    {
        !is_login() && $this->referer('User/login',U('Order/carts'));
        
         $M_cart=M('cart');
        $M_product_image = M('product_image');
        $M_shop=M('shop');
        $M_product=M('product');
        
        $cartid=I('post.cartid');
        empty($cartid)  && redirect(U('order/carts'));
        is_array($cartid) && $cartid=implode(',', $cartid);
        $map=array();
        $map['id']=array('in',$cartid);
        $map['status']=1;
        
        $cart_list=$M_cart->where($map)->select(); 
       
        //购物车中商品id
      
       $identify_price=0; 
       $totalprice=0;
        foreach ($cart_list  as &$item){           
           $images = $M_product_image->where(array('product_id'=>$item['product_id']))->find();
           $item['image_path']= get_cover($images['cover_id'], 'path');
           $tem_pro=$M_product->where(array('id'=>$images['product_id']))->field('price,title,category_id')->find();
           $item['productd_title']=$tem_pro['title'];
           $item['unit_price']=$item['price'];
           $totalprice+=(double)$item['total_price'];
           $identify_price += (double)get_table_field($tem_pro['category_id'],'product_category_id','price','identify') | 0;
       }
        
        $this->assign('identify_price',$identify_price);
        $this->assign('totalprice',$totalprice);
        
        $this->assign('product_list',$cart_list);
       
        $M_member_receive=M('member_receive');
        $receive_list=$M_member_receive->where(array('uid'=>UID))->select();
        //鉴定费
        
        foreach($receive_list as &$item){
            $item['address_full']=
            get_table_field($item['province_id'],'province_id','province','hat_province')
            .get_table_field($item['city_id'],'city_id','city','hat_city')
            .get_table_field($item['area_id'],'area_id','area','hat_area');
        }
        $this->assign('receive_list',$receive_list);
      
       
        $this->display();
    }
    
    /**
     * ajax 修改购物车数量
     */
    public function modifyQuantity(){
        $id=I('request.id');
        $quantity=I('request.quantity');
        $product_id=I('request.product_id');
       
        $M_cart=M('cart');
        $map=array('id'=>$id);
        $procut_info=M('product')->field('price')->find($product_id);
        $price=$procut_info['price'] * $quantity;
        
        $M_cart->where($map)->save(array('quantity'=>$quantity,'total_price'=>$price))
            ? $this->ajaxReturn(1) : $this->ajaxReturn(-1);
    }
    
    /**
     * ajax 删除
     */
    public function delcart(){
        !is_login() && $this->referer(U('User/login'), U('Order/carts'));
        $id=I('request.id');
        $M_cart=M('cart');
        $cart_id=I('post.cart_id');
        
        $M_cart->where(array('id'=>$id))->setfield('status',0) !== false ? $this->success('删除成功') : $this->error('删除失败');
    }
    
    /**
     * 订单支付
     *
     * @author Ringo Chan <ringochan@jxlnxx.com>
     */
    public function pay ()    {
        
            !is_login() && $this->referer('User/login');
            $total=0;
            $order_id=I('request.order_id');
            $order_no=I('request.order_no');
            $checkup_id=I('request.checkup_id');
            $checkup_no=I('request.checkup_no');
            
            $M_payment = M('payment');
            $paymentlist = $M_payment->select();
            $this->assign('paymentlist',$paymentlist);
            
            if($order_id){
            $total=M('order')->where(array('id'=>array('in',$order_id)))->sum('price');
            $this->assign('order_id',$order_id);
            $this->assign('order_no',$order_no);
            }else if($checkup_id){
                $total=get_table_field($checkup_id,'id','total','checkup');
                $this->assign('checkup_id',$checkup_id);
                $this->assign('checkup_no',$checkup_no);
            }
            $this->assign('sum_order_price',$total);
        
        $this->display();
    }
    
    /**
     *支付结果页 
     */
    public function  payresult(){
        $this->display();
    }
    
    /**
     * 直接保存订单
     */
    public function submitorder(){
        if (IS_POST) {
            $quantity = I('post.quantity');
            $product_id = I('post.product_id');
            $M_product_image=M('product_image');
            $M_product=M("product");
            $M_cart=M('cart');
            $product_info=$M_product->field('title,price,category_id')->find($product_id);        
        
            $data=array();
            $data['productd_title']=$product_info['title'];
            $data['quantity']=$quantity;
            $data['price']=$quantity*$product_info['price'];
            $data['product_id']=$product_id;
            $data['unit_price']=$product_info['price'];
            $data['uid']=UID;
            $data['create_time']=NOW_TIME;
            $data['update_time']=NOW_TIME;
            $data['status']=1;
            $data['option_ids']=0;
            $data['product_attr']='默认';
            $identify_price = (double)get_table_field($product_info['category_id'],'product_category_id','price','identify') | 0;
           
            
            $images = $M_product_image->where(array('product_id'=>$product_id))->find();
            $data['image_path']= get_cover($images['cover_id'], 'path');
            
            $this->assign('identify_price',$identify_price);
            $this->assign('totalprice',$data['price']);
            
            $this->assign('product_list',array($data));
             
            //地址
            $M_member_receive=M('member_receive');
            $receive_list=$M_member_receive->where(array('uid'=>UID))->select();
           
            foreach($receive_list as &$item){
                $item['address_full']=
                get_table_field($item['province_id'],'province_id','province','hat_province')
                .get_table_field($item['city_id'],'city_id','city','hat_city')
                .get_table_field($item['area_id'],'area_id','area','hat_area');
            }
            $this->assign('receive_list',$receive_list);           
           
            $this->display();
        }
    }
    

}
?>